﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.IO.Ports;

namespace CarBlackBox
{
    public partial class Form_SetRecorder : Form
    {
        VehicleInfoData Vehicle;
        public Form_SetRecorder(VehicleInfoData Vehicle)
        {
            InitializeComponent();
            this.Vehicle = Vehicle;           
        }

        private void Form_SetRecorder_Load(object sender, EventArgs e)
        {
            foreach (DriveInfo d in DriveInfo.GetDrives())
            {
                if (d.DriveType == DriveType.Removable)
                    cbU.Items.Add(d.Name);
            }
            if(cbU.Items.Count>0)
                cbU.SelectedIndex = 0;
            string[] ports = SerialPort.GetPortNames();
            Array.Sort(ports);
            cbCom.Items.Clear();
            cbCom.Items.Add("默认端口");
            cbCom.Items.AddRange(ports);
            cbCom.SelectedIndex = 0;
            SetVehicleInfoData();
        }
        private void GetVehicleInfoData()
        {            
            Vehicle.LimitSpeed=Convert.ToByte(nLimitSpeed.Value);
            Vehicle.SuperTime= Convert.ToInt16(nSuperTime.Value);
            Vehicle.RecordTime = Convert.ToByte(nRecordTime.Value);
            Vehicle.RestTime = Convert.ToByte(nRestTime.Value);
            Vehicle.TiredTime = Convert.ToByte(nTiredTime.Value);
        }
        private void SetVehicleInfoData()
        {
            tbLicensePlate.Text = Vehicle.LicensePlate;
            tbVIN.Text = Vehicle.VIN;
            DataTable dt= carTypeTableAdapter1.GetDataBy(Vehicle.CarTypeID);
            CarTypeData ctd = CarTypeDataFactory.Construct(dt);
            tbCarType.Text = ctd.Name;
            tbRIN.Text = Vehicle.RIN;
            tbFactor.Text = Vehicle.Factor.ToString();
            tbSensor.Text = Vehicle.Sensor.ToString();
            tbKValue.Text = (Vehicle.Factor * Vehicle.Sensor).ToString();
            nLimitSpeed.Value = Convert.ToDecimal(Vehicle.LimitSpeed);
            nSuperTime.Value = Convert.ToDecimal(Vehicle.SuperTime);
            nRecordTime.Value = Convert.ToDecimal(Vehicle.RecordTime);
            nRestTime.Value = Convert.ToDecimal(Vehicle.RestTime);
            nTiredTime.Value = Convert.ToDecimal(Vehicle.TiredTime);
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            cbU.Items.Clear();            
            foreach (DriveInfo d in DriveInfo.GetDrives())
            {
                if (d.DriveType == DriveType.Removable || !checkBox1.Checked)
                    cbU.Items.Add(d.Name);
            }
            if (cbU.Items.Count > 0)
                cbU.SelectedIndex = 0;
        }

        private void btSetByU_Click(object sender, EventArgs e)
        {
            GetVehicleInfoData();
            int n;
            n = vehicleInfoTableAdapter1.UpdateQuery(Vehicle.VIN,
                                                        Vehicle.CarTypeID,
                                                        Vehicle.Factor,
                                                        Vehicle.Sensor,
                                                        Convert.ToDecimal(Vehicle.OilPer),
                                                        Vehicle.RIN,
                                                        Vehicle.Brand,
                                                        Vehicle.Model,
                                                        Vehicle.BuyDate,
                                                        Vehicle.CheckMonth,
                                                        Vehicle.Memo,
                                                        Vehicle.Sign,
                                                        Vehicle.LimitSpeed,
                                                        Vehicle.RecordTime,
                                                        Vehicle.SuperTime,
                                                        Vehicle.TiredTime,
                                                        Vehicle.Setup,
                                                        Vehicle.RestTime,
                                                        Vehicle.Operator,
                                                        Vehicle.LastUpdateTime,
                                                        Vehicle.LicensePlate
                                                    );
            if (n != 1)
                MessageBox.Show("存储失败，请重新尝试或联系管理员!!!!");
            WFNetLib.Log.SystemLogData log = new WFNetLib.Log.SystemLogData();
            log.LogType = LogType.设置记录仪.ToString();
            log.LogContent = "通过U盘设置信息";
            log.LogRemark = "记录仪" + tbRIN.Text;
            log.InserSQLLog();
        }

        private void btSetTime_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            SerialCommunication com;
            if (cbCom.SelectedIndex == 0)
                com = new SerialCommunication(this);
            else
                com = new SerialCommunication(cbCom.Text,this);
            com.ProcCommand(CommunicationCommand.设置记录仪时钟,dtpDate.Value, dtpTime.Value);
            this.Cursor = Cursors.Default;
            WFNetLib.Log.SystemLogData log = new WFNetLib.Log.SystemLogData();
            log.LogType = LogType.设置记录仪.ToString();
            log.LogContent = "串口设置:设置记录仪时钟:" + dtpDate.Value.ToLongDateString() + "," + dtpTime.Value.ToLongTimeString();
            log.LogRemark = "记录仪" + tbRIN.Text;
            log.InserSQLLog();
        }

        private void btSetFactor_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            SerialCommunication com;
            if (cbCom.SelectedIndex == 0)
                com = new SerialCommunication(this);
            else
                com = new SerialCommunication(cbCom.Text, this);
            com.ProcCommand(CommunicationCommand.设置车辆特征系数,Convert.ToUInt32(tbFactor.Text));
            this.Cursor = Cursors.Default;
            WFNetLib.Log.SystemLogData log = new WFNetLib.Log.SystemLogData();
            log.LogType = LogType.设置记录仪.ToString();
            log.LogContent = "串口设置:设置车辆特征系数:" + tbFactor.Text;
            log.LogRemark = "记录仪" + tbRIN.Text;
            log.InserSQLLog();
        }

        private void btSetVehicleInfo_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            SerialCommunication com;
            if (cbCom.SelectedIndex == 0)
                com = new SerialCommunication(this);
            else
                com = new SerialCommunication(cbCom.Text, this);
            /*object[] ret=*/com.ProcCommand(CommunicationCommand.设置记录仪中的车辆VIN号_车牌号码_分类,tbVIN.Text, tbLicensePlate.Text, tbCarType.Text);
//             if (ret != null && ret.Length == 1)
//             {
//                 if (((string)ret[0]) == "OK")
//                     MessageBox.Show("设置成功");
//             }
            this.Cursor = Cursors.Default;
            WFNetLib.Log.SystemLogData log = new WFNetLib.Log.SystemLogData();
            log.LogType = LogType.设置记录仪.ToString();
            log.LogContent = "串口设置:设置记录仪中的车辆VIN号_车牌号码_分类:" + tbLicensePlate.Text + "," + tbLicensePlate.Text + "," + tbLicensePlate.Text;
            log.LogRemark = "记录仪" + tbRIN.Text;
            log.InserSQLLog();
        }
    }
}
